package com.neusoft.elmboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.NonNull;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class UploadConfiguration extends WebMvcConfigurationSupport {
    
    @Value("${web.upload-path}")
    private String uploadPath;
    
    @Override
    protected void addResourceHandlers(@NonNull ResourceHandlerRegistry registry) {
        // 配置图片访问路径，从配置文件读取上传路径
        // 使用 /img/** 路径映射上传的图片文件
        // 这样访问 http://localhost:8888/img/2024-10-26/xxx.jpg 就能访问到上传的图片
        registry.addResourceHandler("/img/**")
                .addResourceLocations("file:" + uploadPath);
        super.addResourceHandlers(registry);
    }
}
